package com.cnpinxi.service;

import com.cnpinxi.dao.ExpressInfoMapper;
import com.cnpinxi.domain.ExpressInfo;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import com.cnpinxi.tool.KdniaoSubscribeAPI;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-17.
 */
@Service
public class ExpressInfoServiceImpl implements com.cnpinxi.service.ExpressInfoService {
    @Autowired
    ExpressInfoMapper expressInfoMapper;
    private void addExpressInfo(ExpressInfo expressInfo) {

            expressInfoMapper.insertExpressInfo(expressInfo);



    }


    private void updateExpressInfo(ExpressInfo expressInfo) {
        expressInfoMapper.updateByPrimaryKeySelective(expressInfo);
    }


    @Override
    public void modifyExpressInfo(ExpressInfo expressInfo) {
        if (expressInfo.getCreateTime() == null) {
            expressInfo.setCreateTime(new Date());
        }

        Map<String, String> map = new HashMap<>();
        map.put("orderId", String.valueOf(expressInfo.getOrderId()));
        ExpressInfo expressInfo1 = expressInfoMapper.selectExpressInfo(map);
        if (expressInfo1 == null) {
            this.addExpressInfo(expressInfo);
        } else {

            String expressName = expressInfo.getExpressName();
            if (StringUtils.isNotBlank(expressName)) {
                expressInfo1.setExpressName(expressName);
            }
            String expressCode = expressInfo.getExpressCode();
            if (StringUtils.isNotBlank(expressCode)) {
                expressInfo1.setExpressCode(expressCode);
            }
            String expressOrderId = expressInfo.getExpressOrderId();
            if (StringUtils.isNotBlank(expressOrderId)) {
                expressInfo1.setExpressOrderId(expressOrderId);
            }
            String info = expressInfo.getInfo();
            if (StringUtils.isNotBlank(info)) {
                expressInfo1.setInfo(info);
            }

            updateExpressInfo(expressInfo1);
        }
    }

    @Override
    public ExpressInfo selectExpressInfo(Map<String, String> map) {
        return expressInfoMapper.selectExpressInfo(map);
    }


    @Override
    public String checkLogistics(Long orderItemId) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("orderItemId", String.valueOf(orderItemId));
        ExpressInfo expressInfo = expressInfoMapper.selectExpressInfo(map);
        if (expressInfo == null) {
            throw new MyRunTimeExcption("还没有发货！");
        }
        String expCode = expressInfo.getExpressCode();
        String expNo = expressInfo.getExpressOrderId();
        return KdniaoSubscribeAPI.getOrderTracesByJson(expCode, expNo);
    }

    @Override
    public List<ExpressInfo> selectExpressInfosTask() {
        return expressInfoMapper.selectExpressInfosTask();
    }
}
